{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "2005-02-22 是这一年的第 53 天\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000中有 168 个素数，素数分别是: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n",
      "1000中有 168 个素数，素数有：[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "3*5=\n",
      "3*5\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "45123 是 5 位数\n",
      "逆序打印出各位数字: 32154\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "无效的题目编号，请重新输入。\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "最后留下的是原来第11号的那位\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "插入后的数组是:\n",
      "1\n",
      "3\n",
      "5\n",
      "6.0\n",
      "7\n",
      "9\n",
      "11\n",
      "13\n",
      "15\n",
      "17\n",
      "19\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "1000以内的完数有： [6, 28, 496]\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n",
      "第1个月的兔子总数为：1\n",
      "第2个月的兔子总数为：1\n",
      "第3个月的兔子总数为：1\n",
      "第4个月的兔子总数为：2\n",
      "第5个月的兔子总数为：3\n",
      "第6个月的兔子总数为：5\n",
      "第7个月的兔子总数为：8\n",
      "第8个月的兔子总数为：13\n",
      "第9个月的兔子总数为：21\n",
      "第10个月的兔子总数为：34\n",
      "第11个月的兔子总数为：55\n",
      "第12个月的兔子总数为：89\n",
      "\n",
      "请选择题目编号（1-20）运行相应程序，或输入'q'退出:\n"
     ]
    }
   ],
   "source": [
    "# 导入必要的库\n",
    "import datetime\n",
    "import math\n",
    "\n",
    "# question1：输入某年某月某日，判断这一天是这一年的第几天？\n",
    "def question1():\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    date = datetime.date(year, month, day)\n",
    "    print(f\"{date} 是这一年的第 {date.timetuple().tm_yday} 天\")\n",
    "\n",
    "# question2：输出9x9乘法口诀表\n",
    "def question2():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i * j}\", end=\"\\t\")\n",
    "        print()\n",
    "\n",
    "# question3：判断1000中有多少个素数，并输出所有素数\n",
    "def question3():\n",
    "    def is_prime(num):\n",
    "        if num < 2:\n",
    "            return False\n",
    "        for i in range(2, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    primes = [num for num in range(2, 1001) if is_prime(num)]\n",
    "    print(f\"1000中有 {len(primes)} 个素数，素数分别是: {primes}\")\n",
    "    \n",
    "    primes = [i for i in range(1, 1001) if is_prime(i)]\n",
    "    print(f\"1000中有 {len(primes)} 个素数，素数有：{primes}\")\n",
    "\n",
    "# question4：输入一个任意正整数，输出分解质因数\n",
    "def question4():\n",
    "    num = int(input(\"请输入一个正整数: \"))\n",
    "    factors = []\n",
    "    i = 2\n",
    "    while i * i <= num:\n",
    "        while (num % i) == 0:\n",
    "            factors.append(i)\n",
    "            num //= i\n",
    "        i += 1\n",
    "    if num > 1:\n",
    "        factors.append(num)\n",
    "    print(f\"{factors[0]}\", end=\"\")\n",
    "    for factor in factors[1:]:\n",
    "        print(f\"*{factor}\", end=\"\")\n",
    "    print(\"=\")\n",
    "    print(\"*\".join(map(str, factors)))\n",
    "\n",
    "# question5：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示\n",
    "def question5():\n",
    "    def grade_converter(score):\n",
    "        if score >= 90:\n",
    "            print(\"A\")\n",
    "        if 60 <= score <= 89:\n",
    "                print(\"B\")\n",
    "        else:\n",
    "                print(\"C\")\n",
    "\n",
    "# question6：输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字\n",
    "def question6():\n",
    "    num = int(input(\"请输入一个不多于6位的正整数: \"))\n",
    "    num_str = str(num)\n",
    "    digit_count = len(num_str)\n",
    "    reversed_num_str = num_str[::-1]\n",
    "    print(f\"{num} 是 {digit_count} 位数\")\n",
    "    print(f\"逆序打印出各位数字: {reversed_num_str}\")\n",
    "\n",
    "# question7：输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def question7():\n",
    "    def count_characters(s):\n",
    "        letters = 0\n",
    "        spaces = 0\n",
    "        digits = 0\n",
    "        others = 0\n",
    "        for char in s:\n",
    "            if char.isalpha():\n",
    "                letters += 1\n",
    "            if char.isspace():\n",
    "                    spaces += 1\n",
    "            if char.isdigit():\n",
    "                    digits += 1\n",
    "            else:others += 1\n",
    "    print(f\"英文字母: {letters}, 空格: {spaces}, 数字: {digits}, 其它字符: {others}\")\n",
    "\n",
    "# question8：求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制\n",
    "def question8():\n",
    "    def sum_of_repeated_numbers(a, n):\n",
    "        total = 0\n",
    "        num_str = \"\"\n",
    "        for i in range(n):\n",
    "            num_str += str(a)\n",
    "            total += int(num_str)\n",
    "        return total\n",
    "    a = int(input(\"请输入数字a: \"))\n",
    "    n = int(input(\"请输入要累加的次数n: \"))\n",
    "    result = sum_of_repeated_numbers(a, n)\n",
    "    print(f\"结果是: {total}\")\n",
    "\n",
    "# question9：找出1000以内的所有完数。\n",
    "def question9():\n",
    "    def find_factors(n):\n",
    "        \"\"\"返回n的所有真因子\"\"\"\n",
    "        factors = []\n",
    "        for i in range(1, n):\n",
    "            if n % i == 0:\n",
    "                factors.append(i)\n",
    "        return factors\n",
    "    def is_perfect_number(n):\n",
    "        \"\"\"判断一个数是否为完数\"\"\"\n",
    "        return sum(find_factors(n)) == n\n",
    "    def find_perfect_numbers(limit):\n",
    "        \"\"\"找出指定范围内的所有完数\"\"\"\n",
    "        perfect_numbers = []\n",
    "        for num in range(2, limit + 1):\n",
    "            if is_perfect_number(num):\n",
    "                perfect_numbers.append(num)\n",
    "        return perfect_numbers\n",
    "    perfect_numbers = find_perfect_numbers(1000)\n",
    "    print(\"1000以内的完数有：\", perfect_numbers)\n",
    "\n",
    "# question10：有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入\n",
    "def question10():\n",
    "    def fraction_sequence_sum(n):\n",
    "        a, b = 2, 1\n",
    "        total_sum = 0\n",
    "        for _ in range(n):\n",
    "            total_sum += a / b\n",
    "            a, b = a + b, a\n",
    "            print(f\"该数列的前{N}项之和为: {result}\")\n",
    "\n",
    "# question11：输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "def question11():\n",
    "    import math\n",
    "    def factorial(n):\n",
    "        \"\"\"计算n的阶乘\"\"\"\n",
    "        if n == 0 or n == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return math.gamma(n + 1)  # 使用伽玛函数来计算阶乘\n",
    "    def sum_of_factorials(N):\n",
    "        \"\"\"计算1 + 2! + 3! + ... + N!的值\"\"\"\n",
    "        total_sum = 0\n",
    "        for i in range(1, int(math.floor(N)) + 1):\n",
    "            total_sum += factorial(i)\n",
    "        return total_sum\n",
    "    N = float(input(\"请输入一个实数N: \"))\n",
    "    result = sum_of_factorials(N)\n",
    "    print(f\"1 + 2! + 3! + ... + {int(math.floor(N))}! 的值是: {result}\")\n",
    "\n",
    "# question12：一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n",
    "def question12():\n",
    "    def is_perfect_square(n):\n",
    "        root = int(n ** 0.5)\n",
    "        return root * root == n\n",
    "    for x in range(10**6):  # 假设整数不会太大，这里取一个合理的上限\n",
    "        if is_perfect_square(x + 100) and is_perfect_square(x + 100 + 268):\n",
    "            print(f\"满足条件的整数是: {x}\")\n",
    "            break  # 找到答案后退出循环\n",
    "\n",
    "# question13：输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "def question13():\n",
    "    def print_diamond(n):\n",
    "        if n % 2 == 0:\n",
    "            print(\"请输入一个奇数\")\n",
    "            return\n",
    "        mid = n // 2\n",
    "        for i in range(n):\n",
    "            if i <= mid:\n",
    "                print(' ' * (mid - i) + '*' * (2 * i + 1))\n",
    "        else:\n",
    "            print(' ' * (i - mid) + '*' * (2 * (n - i) - 1))\n",
    "\n",
    "\n",
    "# question14：有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n",
    "def question14():\n",
    "    def rabbit_count(months):\n",
    "        if months == 1 or months == 2 or months == 3:\n",
    "            return 1\n",
    "        else:\n",
    "            a, b = 1, 1\n",
    "            for i in range(4, months + 1):\n",
    "                a, b = b, a + b\n",
    "            return b\n",
    "    for month in range(1, 13):\n",
    "        print(f\"第{month}个月的兔子总数为：{rabbit_count(month)}\")\n",
    "\n",
    "# question15：对10个数进行排序。\n",
    "def question15():\n",
    "    def sort_numbers():\n",
    "        numbers = []\n",
    "    for _ in range(10):\n",
    "        num = float(input(\"请输入一个数字: \"))\n",
    "        numbers.append(num)\n",
    "    numbers.sort()\n",
    "    print(\"排序后的数字为：\", numbers)\n",
    "\n",
    "\n",
    "# question16：任意输入一个3x3矩阵，求矩阵对角线元素之和。\n",
    "def question16():\n",
    "    def diagonal_sum_3x3():\n",
    "        matrix = []\n",
    "    for _ in range(3):\n",
    "        row = list(map(int, input(\"请输入一行3个整数，以空格分隔: \").split()))\n",
    "        matrix.append(row)\n",
    "    diagonal_sum = matrix[0][0] + matrix[1][1] + matrix[2][2]\n",
    "    print(f\"矩阵对角线元素之和为：{diagonal_sum}\")\n",
    "\n",
    "# question17：有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n",
    "def question17():\n",
    "    def insert_into_sorted_array(arr, num):\n",
    "        left, right = 0, len(arr) - 1\n",
    "        while left <= right:\n",
    "            mid = (left + right) // 2\n",
    "            if arr[mid] < num:\n",
    "                left = mid + 1\n",
    "            else:\n",
    "                right = mid - 1\n",
    "        arr.insert(left, num)\n",
    "        return arr\n",
    "    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
    "    num = float(input(\"请输入一个数: \"))\n",
    "    new_array = insert_into_sorted_array(sorted_array, num)\n",
    "    print(\"插入后的数组是:\")\n",
    "    for element in new_array:\n",
    "        print(element)\n",
    "\n",
    "# question18：有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数\n",
    "def question18(): \n",
    "    def rotate_list(numbers, m):\n",
    "        m = m % len(numbers)\n",
    "    return numbers[m:] + numbers[:m]\n",
    "\n",
    "# question19：有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n",
    "def question19():\n",
    "    def josephus(n, k):\n",
    "        if n == 1:\n",
    "            return 0\n",
    "        else:\n",
    "            return (josephus(n - 1, k) + k) % n\n",
    "    n = int(input(\"请输入总人数n: \"))\n",
    "    k = 3  # 从1到3报数，即k=3\n",
    "    last_person = josephus(n, k) + 1  # 加1是因为返回的是索引，而题目中编号是从1开始的\n",
    "    print(f\"最后留下的是原来第{last_person}号的那位\")\n",
    "\n",
    "# question20：编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n",
    "def question20():\n",
    "    def sum_even_series(n):\n",
    "        return sum(1 / i for i in range(2, n + 1, 2))\n",
    "    def sum_odd_series(n):\n",
    "        return sum(1 / i for i in range(1, n + 1, 2))\n",
    "    def calculate_series(n):\n",
    "        if n % 2 == 0:\n",
    "            series_sum_func = sum_even_series\n",
    "        else:\n",
    "            series_sum_func = sum_odd_series\n",
    "        return series_sum_func(n)\n",
    "    if __name__ == \"__main__\":\n",
    "        n = int(input(\"请输入一个整数n: \"))\n",
    "        result = calculate_series(n)\n",
    "        print(f\"结果是: {result}\")\n",
    "\n",
    "# 定义一个函数列表，将题目函数与题目编号关联\n",
    "functions = {\n",
    "    \"1\": question1,\n",
    "    \"2\": question2,\n",
    "    \"3\": question3,\n",
    "    \"4\": question4,\n",
    "    \"5\": question5,\n",
    "    \"6\": question6,\n",
    "    \"7\": question7, \n",
    "    \"8\": question8,\n",
    "    \"9\": question9,\n",
    "    \"10\": question10,\n",
    "    \"11\": question11,\n",
    "    \"12\": question12,\n",
    "    \"13\": question13,\n",
    "    \"14\": question14,\n",
    "    \"15\": question15,\n",
    "    \"16\": question16,\n",
    "    \"17\": question17,\n",
    "    \"18\": question18,\n",
    "    \"19\": question19,\n",
    "    \"20\": question20\n",
    "}\n",
    "\n",
    "# 交互选择结构\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"\\n请选择题目编号（1-20）运行相应程序，或输入'q'退出:\")\n",
    "        choice = input(\"输入题目编号: \")\n",
    "        if choice.lower() == 'q':\n",
    "            break\n",
    "        if choice in functions:\n",
    "            functions[choice]()\n",
    "        else:\n",
    "            print(\"无效的题目编号，请重新输入。\")\n",
    "\n",
    "# 运行主程序\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
